package handler

import (
	"ecomm-manage/cache"
	"ecomm-manage/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func GoodOrderDetail(c *gin.Context) {
	id := c.Query("id")
	/*
		SELECT
		  good_orders.id OrderId,
		  goods.good_no GoodNo,
		  goods.good_name GoodName,
		  goods.good_price GoodPrice,
		  good_orders.order_no OrderNo,
		  good_orders.order_price OrderPrice
		FROM
		  good_orders
		  LEFT JOIN goods ON good_orders.gid = goods.id
		WHERE good_orders.id=1
	*/
	var goodOrderList model.GoodOrderList
	redis_key := "good_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("good_orders").
			Select(
				"good_orders.id OrderId",
				"goods.good_no GoodNo",
				"goods.good_name GoodName",
				"goods.good_price GoodPrice",
				"good_orders.order_no OrderNo",
				"good_orders.order_price OrderPrice",
			).
			Joins("LEFT JOIN goods ON good_orders.gid = goods.id").
			Where("good_orders.id=?", id).Find(&goodOrderList)
		jsonData, _ := json.Marshal(goodOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &goodOrderList)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": goodOrderList,
	})
	return
}

func GoodOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var total int64
	model.DB.Model(&model.GoodOrder{}).Count(&total)
	/*
		SELECT
		  good_orders.id OrderId,
		  goods.good_no GoodNo,
		  goods.good_name GoodName,
		  goods.good_price GoodPrice,
		  good_orders.order_no OrderNo,
		  good_orders.order_price OrderPrice
		FROM
		  good_orders
		  LEFT JOIN goods ON good_orders.gid = goods.id
	*/
	var goodOrderList []model.GoodOrderList
	redis_key := "good_order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("good_orders").
			Select(
				"good_orders.id OrderId",
				"goods.good_no GoodNo",
				"goods.good_name GoodName",
				"goods.good_price GoodPrice",
				"good_orders.order_no OrderNo",
				"good_orders.order_price OrderPrice",
			).
			Joins("LEFT JOIN goods ON good_orders.gid = goods.id").
			Offset(offset).Limit(size).Find(&goodOrderList)
		jsonData, _ := json.Marshal(goodOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &goodOrderList)
	}
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  goodOrderList,
		"total": total,
	})
	return
}

func GoodOrderAdd(c *gin.Context) {
	gid, _ := strconv.Atoi(c.PostForm("gid"))
	order_no := c.PostForm("order_no")
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	data := model.GoodOrder{
		Gid:        gid,
		OrderNo:    order_no,
		OrderPrice: order_price,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func GoodAdd(c *gin.Context) {
	good_no := c.PostForm("good_no")
	good_name := c.PostForm("good_name")
	good_price, _ := strconv.Atoi(c.PostForm("good_price"))
	data := model.Good{
		GoodNo:    good_no,
		GoodName:  good_name,
		GoodPrice: good_price,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
